package com.sxzbxc.weixin.controller;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sxzbxc.weixin.entity.DicCompanies;
import com.sxzbxc.weixin.entity.DicMelfunction;
import com.sxzbxc.weixin.entity.SysUser;
import com.sxzbxc.weixin.entity.infoMelfunction;
import com.sxzbxc.weixin.pojo.AjaxJson;
import com.sxzbxc.weixin.service.DicCompaniseServiceI;
import com.sxzbxc.weixin.service.DicMelfunctionServiceI;
import com.sxzbxc.weixin.service.InfoMelfunctionServiceI;

/**
 * 功能：
 * 	1.主要自动生成故障类型的页面
 * 	2.根据不同的故障类型返回不同的页面
 * @author Tom
 *
 */
@Controller
@RequestMapping("infomelController")
public class InfomelController {

	@Autowired
	private DicMelfunctionServiceI dicMelfunctionService;//工单类型
	
	@Autowired
	private InfoMelfunctionServiceI infoMelfunctionService;//工单信息
	
	@Autowired
	private DicCompaniseServiceI dicCompaniseService;//企业部门
	
	
	@RequestMapping(params = "toMelfunction")
	public ModelAndView toMelfunction(){
		
		return new ModelAndView("melfunction/melfunctionList");
	}
	
	@RequestMapping(params = "datagrid")
	@ResponseBody
	public AjaxJson datagrid(){
		AjaxJson j = new AjaxJson();
		List<DicMelfunction> loadAll = dicMelfunctionService.loadAll();
		Map<String , String > map = new HashMap<String , String>();
		for (DicMelfunction dicMelfunction : loadAll) {
			if(dicMelfunction.getDicStatus()==1){
				String key = dicMelfunction.getDicMelcode();
				String value = dicMelfunction.getDicMelname();
				map.put(key, value);
			}
		}
		j.setObj(map);
		j.setSuccess(true);
		return j;
	}
	
	/**
	 * 故障反馈表单
	 * @return
	 */
	@RequestMapping(params = "toAdd")
	public ModelAndView toAdd(String melCode){
		
		return new ModelAndView("melfunction/toAdd");
	}
	
	@RequestMapping(params = "doAdd")
	@ResponseBody
	public AjaxJson doAdd(HttpServletRequest request , infoMelfunction infoMelfunction){
		AjaxJson j = new AjaxJson();
		try {
			Date date = new Date();  
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
	        String dateTime = df.format(date);
	        Date date1 = null;
	        try {
	            date1 = df.parse(dateTime);
	        } catch (ParseException e) {
	            e.printStackTrace();
	        }
			infoMelfunction.setInfoCreateDate(date1);
			infoMelfunctionService.insertSelective(infoMelfunction);
			j.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setSuccess(false);
		}
		return j;
	}
	
	/**
	 * 故障反馈
	 * @param request
	 * @param response
	 * @param infoMel
	 * @return
	 */
	@RequestMapping(params = "doUpdate")
	@ResponseBody
	public AjaxJson doUpdate(HttpServletRequest request , HttpServletResponse response,String id, String infoSubstation,String infoLinecode,String infoRegion,String infoReason,int infoDisposeStatus,int infoClinetFeedback){
		AjaxJson j = new AjaxJson();
		SysUser user = (SysUser)request.getSession().getAttribute("user");
		try {
			infoMelfunction info = infoMelfunctionService.selectByPrimaryKey(id);
			info.setInfoSubstation(infoSubstation);
			info.setInfoLinecode(infoLinecode);
			info.setInfoRegion(infoRegion);
			info.setInfoReason(infoReason);
			info.setInfoDisposeStatus(infoDisposeStatus);
			info.setInfoClinetFeedback(infoClinetFeedback);
			info.setInfoUpdateName(user.getUserName());//故障处理人员
			Date date = new Date();  
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
	        String dateTime = df.format(date);
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        Date date1 = null;
	        try {
	            date1 = sdf1.parse(dateTime);
	        } catch (ParseException e) {
	            e.printStackTrace();
	        }
			info.setInfoUpdateDate(date1);//故障处理时间
			info.setInfoStatus(1);
			infoMelfunctionService.updateByPrimaryKeySelective(info);
			j.setSuccess(true);
		} catch (Exception e) {
			j.setSuccess(false);
			e.printStackTrace();
		}
		return j;
	}
	
	
	@RequestMapping(params = "infoDataGrid")
	@ResponseBody
	public AjaxJson infoDataGrid(){
		AjaxJson j = new AjaxJson();
		
		return j;
	}
	
	/**
	 * 根据工单获取数据
	 * @param request
	 * @param wordOrder
	 * @return
	 */
	@RequestMapping(params = "iDDataGrid")
	@ResponseBody
	public AjaxJson iDDataGrid(HttpServletRequest request , String wordOrder){
		AjaxJson j = new AjaxJson();
		try {
			infoMelfunction entity = infoMelfunctionService.selectByPrimaryKey(wordOrder);
			j.setObj(entity);
			j.setSuccess(true);
		} catch (NullPointerException  e) {
			j.setSuccess(false);
			e.printStackTrace();
		}catch (Exception e) {
			j.setSuccess(false);
			e.printStackTrace();
		}
		return j;
	}
	
	
	/**
	 * 根据故障类型来获取数据
	 * @param request
	 * @param value
	 * @return
	 */
	@RequestMapping(params = "codeDataGrid")
	@ResponseBody
	public AjaxJson codeDataGrid(HttpServletRequest request , String value){
		AjaxJson j = new AjaxJson();
		SysUser user =  (SysUser)request.getSession().getAttribute("user");
		String userDepart = user.getUserDepart();//获取单位ID;
		if(userDepart==null){
			j.setSuccess(false);
			j.setMsg("该用户没有所属部门！");
			return j;
		}
		DicMelfunction selectByMelCode = dicMelfunctionService.selectByMelCode(value);
		String id = selectByMelCode.getId();
		List<infoMelfunction> selectByMelType = infoMelfunctionService.selectByMelType(id);
		List<infoMelfunction> info = new ArrayList<infoMelfunction>();
		for (infoMelfunction infoMelfunction : selectByMelType) {
			if(infoMelfunction.getInfoForDepart().equals(userDepart.trim())){
				if(infoMelfunction.getInfoStatus()==0){
					info.add(infoMelfunction);
				}
				
			}
		}
		j.setSuccess(true);
		j.setObj(info);
		return j;
	}
	
	
	@RequestMapping(params = "adminDoInfo")
	@ResponseBody
	public AjaxJson adminDoInfo(HttpServletRequest request){
		AjaxJson j = new AjaxJson();
		Map<String , Object> map = new HashMap<String , Object>();
		List<DicCompanies> loadAll = dicCompaniseService.loadAll();//企业部门
		map.put("infoForDepart", loadAll);
		Date date = new Date();//派单时间
		map.put("infoCreateDate", date);
		SysUser user = (SysUser)request.getSession().getAttribute("user");//派单人员信息
		List<DicMelfunction> loadAll2 = dicMelfunctionService.loadAll();//工单类型
		List<DicMelfunction> dicMelFunction = new ArrayList<DicMelfunction>();
		for (DicMelfunction dicMel : loadAll2) {
			if(dicMel.getDicStatus()==1){
				dicMelFunction.add(dicMel);
			}
			
		}
		map.put("infoCreateName", user);
		map.put("infoMelfunctionType", dicMelFunction);
		j.setObj(map);
		j.setSuccess(true);
		return j;
	}
	
}
